<?php
namespace app\index\service;

use app\index\model\BidModel;
use app\index\model\DealcomModel;
use app\index\model\DeptModel;
use app\index\model\QianPiaoModel;
use think\Db;

class BidService extends CommonService
{
    public function check_bid_name($name, $id=0){
        return !!BidModel::where(['comid'=>$this->userinfo['comid'],'name'=>$name,'deleted_user'=>0,'isturn'=>1])
            ->where('status','in', [0,1])
            ->where('id','neq', $id)->find();
    }


    public function get_user_bid(){
        $where = Db::raw("created_user=".$this->userinfo['id']." or touer=".$this->userinfo['id']);
        $arr = BidModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where($where)
            ->column('id');
        return $arr;
    }



    public function get_bid_state(){
        return [
            1=>'在投',
            3=>'中标',
            6=>'未中标',
        ];
    }
    public function get_bid_kai_guo(){
        return [
            3=>'中标',
            6=>'未中标',
        ];
    }

    public function get_bid_state_name_wen($data){
        $name = '';
        if($data['status'] == 1){
            if($data['state'] == 1){
                $name = '在投';
            }
            if($data['state'] == 3){
                $name = '中标';
            }
            if($data['state'] == 6){
                $name = '未中标';
            }
        }
        return $name;
    }

    public function get_bid_state_name_tag($data){
        $name = '';
        if($data['status'] == 1){
            if($data['state'] == 1){
                $name = '<span class="colorhuang">在投</span>';
            }
            if($data['state'] == 3){
                $name = '<span class="colorlv">中标</span>';
            }
            if($data['state'] == 6){
                $name = '<span class="colorhui">未中标</span>';
            }
        }
        return $name;
    }




    public function get_bid_info($bidid, $pai_qian_zhi_id=0){
        $data = BidModel::get_info_by_pk($bidid);
        if(!$data){
            return [];
        }

        $data['state_name_wen'] = $this->get_bid_state_name_wen($data);
        $data['lanshi_name'] = (new OptService())->get_base_showname('bid_lanshi', $data['lanshi']);
        $data['kailei_name'] = (new OptService())->get_base_showname('bid_kailei', $data['kailei']);
        $data['gui_unit_name'] = DeptModel::get_name_by_pk($data['gui_unit']);
        $data['qian_count'] = (new QianService())->get_bid_qian_count($bidid, $pai_qian_zhi_id);
        return $data;
    }

    public function get_dealcom_info($name){
        $data = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'name'=>$name])->find();
        if(!$data){
            return [];
        }
        $data['ban_count'] = (new QianService())->get_gofuer_ban_count($data['name']);
        return $data;
    }






}
